<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 卡片区域 -->
      <el-card>
        <el-tabs>
          <el-tab-pane label="角色管理">
            <!-- 左侧内容区域 -->
            <!-- 头部按钮 -->
            <el-row style="height:60px">
              <el-button icon="el-icon-plus" size="small" type="primary" @click="showDialog = true">新增角色</el-button>
            </el-row>
            <!-- 表格区域 -->
            <el-table :data="list" border>
              <el-table-column align="center" label="序号" type="index" width="120" />
              <el-table-column align="center" prop="name" label="角色名" width="240" />
              <el-table-column align="center" prop="description" label="描述" />
              <el-table-column label="操作">
                <template slot-scope="{row}">
                  <el-button type="success" size="small" @click="assignPre(row.id)">分配权限</el-button>
                  <el-button type="primary" size="small" @click="editRole(row.id)">修改</el-button>
                  <el-button type="danger" size="small" @click="deleteRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" style="height:60px" align="middle">
              <el-pagination :current-page="page.page" :page-size="page.pagesize" :total="page.total" layout="prev ,pager, next" @current-change="changePage">1</el-pagination>
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <!-- 右侧内容区域 -->
            <el-alert :closable="false" :show-icon="true" type="info" title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改" />
            <el-form :model="formData">
              <el-form-item label="企业名称" label-width="120px" style="margin-top:50px">
                <el-input v-model="formData.name" style="width:400px" disabled />
              </el-form-item>
              <el-form-item label="公司地址" label-width="120px" style="margin-top:50px">
                <el-input v-model="formData.companyAddress" style="width:400px" disabled />
              </el-form-item>
              <el-form-item label="公司电话" label-width="120px" style="margin-top:50px">
                <el-input v-model="formData.companyPhone" style="width:400px" disabled />
              </el-form-item>
              <el-form-item label="邮箱" label-width="120px" style="margin-top:50px">
                <el-input v-model="formData.mailbox" style="width:400px" disabled />
              </el-form-item>
              <el-form-item label="备注" label-width="120px" style="margin-top:50px">
                <el-input v-model="formData.remarks" type="textarea" style="width:400px" disabled />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog :visible="showDialog" title="编辑角色" width="500px" @close="closeDialog">
        <el-form ref="formRef" :model="dialogForm" :rules="rules" label-width="120px" class="dialog-form">
          <el-form-item label="角色名称" style="margin-right:50px" prop="name">
            <el-input v-model="dialogForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" style="margin-right:50px">
            <el-input v-model="dialogForm.description" />
          </el-form-item>
        </el-form>
        <el-row type="flex" justify="center">
          <el-col :span="8">
            <el-button @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="confirm">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 分配权限弹出层 -->
      <el-dialog title="分配权限" :visible="showPreDialog" @close="btnCancle">
        <el-tree
          ref="treePre"
          :data="preList"
          :props="defaultProps"
          show-checkbox
          default-expand-all
          :check-strictly="true"
          :default-checked-keys="selectChecked"
          node-key="id"
        />
        <el-row slot="footer" type="flex" justify="center">
          <el-button type="primary" @click="btnOk">确定</el-button>
          <el-button @click="btnCancle">取消</el-button>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, deleteRole, getRoleDetail, updateRole, addRole, assignPerm } from '@/api/setting'
import { mapGetters } from 'vuex'
import { getPermissionList } from '@/api/permission'
import { tranListToTreeData } from '@/utils'
export default {
  data() {
    return {
      list: [], // 承接数组
      page: {
        // 放置页码及其相关数据
        page: 1,
        pagesize: 10,
        total: 0// 记录总数
      },
      formData: {
        name: '',
        description: ''
      },
      showDialog: false,
      dialogForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      showPreDialog: false,
      preList: [],
      defaultProps: {
        label: 'name'
      },
      permIds: [],
      roleId: null,
      selectChecked: []
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.page.total = total
      this.list = rows
    },
    changePage(newPage) {
      // newPage是当前点击页码
      this.page.page = newPage// 将当前页码值赋值给最新页码
      this.getRoleList()
    },
    async getCompanyInfo() {
      const res = await getCompanyInfo(this.companyId)
      this.formData = res
    },
    async deleteRole(id) {
      // 成功进then,失败进catch改成了await和async之后需要用try catch捕获异常
      try {
        await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await deleteRole(id)
        this.getRoleList()
        this.$message.success('删除角色成功')
      } catch (error) {
        console.log(error)
      }
    },
    async editRole(id) {
      const res = await getRoleDetail(id)
      this.dialogForm = res
      this.showDialog = true
    },
    closeDialog() {
      this.showDialog = false
      // console.log(this.$refs.formRef)
      this.$refs.formRef.resetFields()
      this.dialogForm = {
        name: '',
        description: ''
      }
    },
    async confirm() {
      try {
        await this.$refs.formRef.validate()
        // dialogForm这个对象有id就是编辑没有id就是新增
        if (this.dialogForm.id) {
          await updateRole(this.dialogForm)
        } else {
          // 新增
          await addRole(this.dialogForm)
        }
        this.getRoleList()
        this.showDialog = false
        this.$message.success('操作成功')
      } catch (error) {
        console.log('填写信息不合法')
      }
    },
    async assignPre(id) {
      this.preList = tranListToTreeData(await getPermissionList(), '0')
      this.roleId = id
      const { permIds } = await getRoleDetail(id)
      this.selectChecked = permIds
      this.showPreDialog = true
    },
    async btnOk() {
      await assignPerm({ id: this.roleId, permIds: this.$refs.treePre.getCheckedKeys() })
      this.$message.success('分配角色权限成功')
      this.showPreDialog = false
      console.log(this.selectChecked)
    },
    btnCancle() {
      this.selectChecked = []
      this.showPreDialog = false
      // console.log(this.selectChecked)
    }
  }
}
</script>

<style lang="scss" scoped>
  .dialog-form{
    margin-bottom: 50px;
  }
  .el-col{
    display: flex;
    justify-content: center;
  }
</style>

